<cfcomponent displayname="snipDAO" hint="I abstract data access for snip.">

	<cffunction name="init" access="public" returntype="snipDAO" output="false" displayname="snip Constructor" hint="I initialize a snip.">
		<cfargument name="datasourceSettings" type="any" required="true" />
		
		<cfset variables.dsn = arguments.datasourceSettings.getConfigSetting("dsn") />
		<cfset variables.dbtype = arguments.datasourceSettings.getConfigSetting("dbtype") />

		<cfreturn this />
	</cffunction>
 
	<cffunction name="save" returntype="any" output="false" access="public" 	hint="I save a snip">
		<cfargument name="snip" type="snip" hint="I am the snip to be saved" required="true"/>

		<cfif arguments.snip.hasId()>
			<cfif arguments.snip.getsnipid() gt 0>
				<cfreturn update(arguments.snip)/>
			<cfelse>
				<cfreturn create(arguments.snip)/>
			</cfif>
		<cfelse>
			<cfreturn create(arguments.snip)/>
		</cfif>

	</cffunction>

	<cffunction name="create" access="remote" returntype="any" output="false">
		<cfargument name="snipBean" type="snip" required="true" hint="I am the snip object into which data is created." />/>

		<cfquery name="insRec" datasource="#variables.dsn#">
			insert into snip(
			name, useremail, parent, help, description, starttext, endtext, author, platforms, template, created, updated, published, ownerid
			)
			values(
				<cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.snipBean.getname()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.snipBean.getuseremail()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_INTEGER" value="#arguments.snipBean.getparent()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_LONGVARCHAR" value="#arguments.snipBean.gethelp()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.snipBean.getdescription()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_LONGVARCHAR" value="#arguments.snipBean.getstarttext()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_LONGVARCHAR" value="#arguments.snipBean.getendtext()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.snipBean.getauthor()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.snipBean.getplatforms()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.snipBean.gettemplate()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_TIMESTAMP" value="#arguments.snipBean.getcreated()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_TIMESTAMP" value="#arguments.snipBean.getupdated()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_INTEGER" value="#arguments.snipBean.getpublished()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.snipBean.getownerid()#"  /> 
				
				)
		</cfquery>

		<cfquery name="qGetID" datasource="#variables.dsn#">
			SELECT
			<cfswitch expression="#variables.dbtype#">
				<cfcase value="mysql">
					LAST_INSERT_ID()
				</cfcase>
				<cfcase value="mssql">
					@@IDENTITY
				</cfcase>
				<cfcase value="msaccess">
					MAX(snipid )
					
				</cfcase>
				<cfdefaultcase>
					<cfthrow message="Unknown Datasource Vendor!">
				</cfdefaultcase>
			</cfswitch>
				AS newid
			FROM snip
		</cfquery>
		
		
			<cfset snipBean.setsnipid(qGetID.newid)>
		
		
		<cfreturn snipBean/>
	</cffunction>
	
	<cffunction name="read" access="public" returntype="any" output="false" hint="CRUD method read.">
		<cfargument name="snipid" type="numeric" required="true" hint="I am the ID of the database record to read." />
		<cfset var snipBean = createobject("component","snip").init() />
		
		<cfquery name="snipSelect" datasource="#variables.dsn#">
			SELECT * 
			FROM	snip	
			WHERE
				snipid = <cfqueryparam cfsqltype="cf_sql_INTEGER" value="#arguments.snipid#"  /> 
				
		</cfquery>

		<cfif snipSelect.recordCount >
			<cfscript>
				snipBean.setsnipid(snipSelect.snipid);				
				snipBean.setname(snipSelect.name);				
				snipBean.setuseremail(snipSelect.useremail);				
				snipBean.setparent(snipSelect.parent);				
				snipBean.sethelp(snipSelect.help);				
				snipBean.setdescription(snipSelect.description);				
				snipBean.setstarttext(snipSelect.starttext);				
				snipBean.setendtext(snipSelect.endtext);				
				snipBean.setauthor(snipSelect.author);				
				snipBean.setplatforms(snipSelect.platforms);				
				snipBean.settemplate(snipSelect.template);				
				snipBean.setcreated(snipSelect.created);				
				snipBean.setupdated(snipSelect.updated);				
				snipBean.setpublished(snipSelect.published);				
				snipBean.setownerid(snipSelect.ownerid);				
				
			</cfscript>			
			
		</cfif>
		
		<cfreturn snipBean />
	</cffunction>

	<cffunction name="update" returntype="any" output="false" hint="CRUD method update">
		<cfargument name="snipBean" type="snip" required="true" />	
		<cfset var snipUpdate = 0 >

		<cfquery name="snipUpdate" datasource="#variables.dsn#" >
			UPDATE snip
			SET
				name = <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.snipBean.getname()#"  /> 
				, useremail = <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.snipBean.getuseremail()#"  /> 
				, parent = <cfqueryparam cfsqltype="cf_sql_INTEGER" value="#arguments.snipBean.getparent()#"  /> 
				, help = <cfqueryparam cfsqltype="cf_sql_LONGVARCHAR" value="#arguments.snipBean.gethelp()#"  /> 
				, description = <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.snipBean.getdescription()#"  /> 
				, starttext = <cfqueryparam cfsqltype="cf_sql_LONGVARCHAR" value="#arguments.snipBean.getstarttext()#"  /> 
				, endtext = <cfqueryparam cfsqltype="cf_sql_LONGVARCHAR" value="#arguments.snipBean.getendtext()#"  /> 
				, author = <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.snipBean.getauthor()#"  /> 
				, platforms = <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.snipBean.getplatforms()#"  /> 
				, template = <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.snipBean.gettemplate()#"  /> 
				, created = <cfqueryparam cfsqltype="cf_sql_TIMESTAMP" value="#arguments.snipBean.getcreated()#"  /> 
				, updated = <cfqueryparam cfsqltype="cf_sql_TIMESTAMP" value="#arguments.snipBean.getupdated()#"  /> 
				, published = <cfqueryparam cfsqltype="cf_sql_INTEGER" value="#arguments.snipBean.getpublished()#"  /> 
				, ownerid = <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.snipBean.getownerid()#"  /> 
				
			WHERE
				snipid = <cfqueryparam cfsqltype="cf_sql_INTEGER" value="#arguments.snipBean.getsnipid()#"  /> 
				
		</cfquery>
		
		<cfreturn snipBean />
	</cffunction>

	<cffunction name="delete" returntype="void" output="false" hint="CRUD method delete" >
		<cfargument name="snipBean" type="snip" required="true" />		

		<cfset var snipDelete = 0 >

		<cfquery name="snipDelete" datasource="#variables.dsn#" >
			DELETE FROM snip
			WHERE
				snipid = <cfqueryparam cfsqltype="cf_sql_INTEGER" value="#arguments.snipBean.getsnipid()#"  /> 
				
		</cfquery>
	</cffunction>

</cfcomponent>